<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport('joomla.filesystem.folder');

class AutogeneratorControllerComponent extends JControllerForm {

    private $oApp;

    /**
     *
     * @var AutogeneratorModelComponent
     */
    private $oModelComponent;

    public function __construct($config = array()) {
        parent::__construct($config);

        $this->oApp = JFactory::getApplication();
        $this->oModelComponent = $this->getModel();
    }

    public function import() {
        $aComponetId = $this->oApp->input->get('cid', array(), 'array');

        $sMessage = JText::_('COM_AUTOGENERATOR_IMPORT_ERROR_MESSAGE_COMPONENT_NOT_VALID');

        if (count($aComponetId) > 0) {
            $this->oApp->redirect('index.php?option=com_autogenerator&view=import&layout=edit&iComponentId=' . (int) $aComponetId[0]);
        } else {
            $this->oApp->redirect('index.php?option=com_autogenerator&view=components', $sMessage, 'error');
        }
    }

    public function changeStatus() {
        $iComponentId = $this->oApp->input->getInt('iComponentId');

        // Get table.
        $sMessage = $this->oModelComponent->changeStatus($iComponentId);

        echo $sMessage;
        die;
    }

    public function exportPHPFile() {
        $oApp = JFactory::getApplication();
        $sFileName = $oApp->input->get('file', 'index.php');
        $bIsAdmin = $oApp->input->getBool('bIsAdmin', true);
        $bIsMain = $oApp->input->getBool('bIsMain', false);
        $iComponentId = $oApp->input->getInt('id');
        $sType = $oApp->input->get('type', 'list');
        $oComponent = $this->oModelComponent->getItem($iComponentId);
        $oUser = JFactory::getUser();

        switch ($sFileName) {
            case 'install.mysql.utf8.sql':
                $sContent = AutogeneratorHelper::getSqlInstallContent($iComponentId);
                break;
            case 'uninstall.mysql.utf8.sql':
                $sContent = AutogeneratorHelper::getSqlUninstallContent($iComponentId);
                break;
            case 'helper.php':
                $sContent = AutogeneratorHelper::getHelperContent($iComponentId);
                break;
            default:
                $aMVCs = AutogeneratorHelper::getAllMVCsByComponentId($iComponentId, ConstantHelper::$_MVC_TYPE_LIST);

                VariableHelper::$_VARIABLES['AUTO_GENERATOR_COMPONENT'] = $oComponent->title;
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR'] = $oUser->name;
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_CREATE_DATE'] = JFactory::getDate()->format('F o');
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_COPYRIGHT'] = $oComponent->copyright;
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_EMAIL'] = $oUser->email;
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_URL'] = 'www.joomla.org';
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_VERSION'] = '3.0.0';
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_INCLUDE_HEADER_TEMPLATE'] = '';
                VariableHelper::$_VARIABLES['AUTO_GENERATOR_DEFAULT_VIEWS'] = isset($aMVCs[0]->title) ? $aMVCs[0]->title : '';

                $sContent = AutogeneratorHelper::getContentFileInTemplate($sFileName, $sType, $bIsAdmin, $bIsMain);
                break;
        }

        $sFileExport = $sFileName;
        switch ($sFileName) {
            case 'component.php':
                $sFileExport = strtolower($oComponent->title) . '.php';
                break;

            case 'component.xml':
                $sFileExport = strtolower($oComponent->title) . '.xml';
                break;

            case 'helper.php':
                $sFileExport = strtolower($oComponent->title) . '.php';
                break;

            default:
                break;
        }

        // Set headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$sFileExport");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");

        echo $sContent;
        die;
    }

    public function exportZipPackage() {
        $oApp = JFactory::getApplication();
        $iComponentId = $oApp->input->getInt('id');
        $oComponent = $this->oModelComponent->getItem($iComponentId);
        $oUser = JFactory::getUser();
        $aTables = AutogeneratorHelper::getAllTablesByComponentId($iComponentId);
        $aMVCs = AutogeneratorHelper::getAllMVCsByComponentId($iComponentId);
        
        $aListMVCs = AutogeneratorHelper::getAllMVCsByComponentId($iComponentId, ConstantHelper::$_MVC_TYPE_LIST);
        
        if (!$aListMVCs) {
            echo 'No list views to export! Please create the list of MVCs first!';
            die;
        }
        
        if (!isset($aListMVCs[0]->title)) {
            echo 'Default view is not valid!';
            die;
        }
        
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_COMPONENT'] = $oComponent->title;
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR'] = $oUser->name;
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_CREATE_DATE'] = JFactory::getDate()->format('F o');
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_COPYRIGHT'] = $oComponent->copyright;
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_EMAIL'] = $oUser->email;
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_URL'] = 'www.joomla.org';
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_VERSION'] = '3.0.0';
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_INCLUDE_HEADER_TEMPLATE'] = '';
        VariableHelper::$_VARIABLES['AUTO_GENERATOR_DEFAULT_VIEWS'] = isset($aListMVCs[0]->title) ? $aListMVCs[0]->title : '';
        
        // Export folder for this component
        $sComponentFolder = 'com_' . strtolower($oComponent->title) . '_' . $oComponent->token;
        
        AutogeneratorHelper::deleteFolder($sComponentFolder);
        AutogeneratorHelper::createFolder($sComponentFolder);
        
        $sAdminFolder = $sComponentFolder . DS . 'administrator' .  DS . 'components' . DS . 'com_' . strtolower($oComponent->title);
        AutogeneratorHelper::createFolder($sAdminFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('component.xml', 'form', true, true);
        AutogeneratorHelper::saveFile(strtolower($oComponent->title) . '.xml', $sContent, $sAdminFolder);
        
        AutogeneratorHelper::$aAdminLanguage['COM_' . strtoupper($oComponent->title)] = $oComponent->title;
        AutogeneratorHelper::$aAdminLanguageSystem['COM_' . strtoupper($oComponent->title)] = $oComponent->title;
        AutogeneratorHelper::$aAdminLanguageSystem['COM_' . strtoupper($oComponent->title). '_XML_DESCRIPTION'] = $oComponent->description;
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('access.xml', 'list', true, true);
        AutogeneratorHelper::saveFile('access.xml', $sContent, $sAdminFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('component.php', 'form', true, true);
        AutogeneratorHelper::saveFile(strtolower($oComponent->title) . '.php', $sContent, $sAdminFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('config.xml', 'list', true, true);
        AutogeneratorHelper::saveFile('config.xml', $sContent, $sAdminFolder);
        
        AutogeneratorHelper::$aAdminLanguageSystem['COM_' . strtoupper($oComponent->title). '_FIELDSET_CONFIG_CLIENT_OPTIONS_LABEL'] = $oComponent->title;
        AutogeneratorHelper::$aAdminLanguageSystem['COM_' . strtoupper($oComponent->title). '_FIELDSET_CONFIG_CLIENT_OPTIONS_DESC'] = $oComponent->description;
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('controller.php', 'list', true, true);
        AutogeneratorHelper::saveFile('controller.php', $sContent, $sAdminFolder);
        
        // Create a controllers folder in admininstrator.
        $sAdminControllersFolder = $sAdminFolder . DS . 'controllers';
        AutogeneratorHelper::createFolder($sAdminControllersFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminControllersFolder);
        
        $sAdminHelpersFolder = $sAdminFolder . DS . 'helpers';
        AutogeneratorHelper::createFolder($sAdminHelpersFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminHelpersFolder);
        
        $sContent = AutogeneratorHelper::getHelperContent($iComponentId);
        AutogeneratorHelper::saveFile('helper.php', $sContent, $sAdminHelpersFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('controller.php', 'list', true, true);
        AutogeneratorHelper::saveFile('controller.php', $sContent, $sAdminFolder);
        
        $sAdminModelsFolder = $sAdminFolder . DS . 'models';
        AutogeneratorHelper::createFolder($sAdminModelsFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminModelsFolder);
        
        $sAdminModelsXMLFormsFolder = $sAdminModelsFolder . DS . 'forms';
        AutogeneratorHelper::createFolder($sAdminModelsXMLFormsFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminModelsXMLFormsFolder);
        
        $sAdminModelsFieldsFolder = $sAdminModelsFolder . DS . 'fields';
        AutogeneratorHelper::createFolder($sAdminModelsFieldsFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminModelsFieldsFolder);
        
        $sAdminSqlFolder = $sAdminFolder . DS . 'sql';
        AutogeneratorHelper::createFolder($sAdminSqlFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminSqlFolder);
        
        $sAdminLanguageFolder = $sAdminFolder . DS . 'language';
        AutogeneratorHelper::createFolder($sAdminLanguageFolder);
        
        $sAdminTablesFolder = $sAdminFolder . DS . 'tables';
        AutogeneratorHelper::createFolder($sAdminTablesFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminTablesFolder);
        
        $sAdminViewsFolder = $sAdminFolder . DS . 'views';
        AutogeneratorHelper::createFolder($sAdminViewsFolder);
        AutogeneratorHelper::copyIndexHTML($sAdminViewsFolder);
        
        $sFrontendFolder = $sComponentFolder . DS . 'components' . DS . 'com_' . strtolower($oComponent->title);
        AutogeneratorHelper::createFolder($sFrontendFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendFolder);

        $sFrontendSqlFolder = $sFrontendFolder . DS . 'sql';
        AutogeneratorHelper::createFolder($sFrontendSqlFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendSqlFolder);
        
        $sFrontendLanguageFolder = $sFrontendFolder . DS . 'language';
        AutogeneratorHelper::createFolder($sFrontendLanguageFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('component.php', 'form', false, true);
        AutogeneratorHelper::saveFile(strtolower($oComponent->title) . '.php', $sContent, $sFrontendFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('controller.php', 'form', false, true);
        AutogeneratorHelper::saveFile('controller.php', $sContent, $sFrontendFolder);

        $sContent = AutogeneratorHelper::getContentFileInTemplate('metadata.xml', 'list', false, true);
        AutogeneratorHelper::saveFile('metadata.xml', $sContent, $sFrontendFolder);
        
        $sContent = AutogeneratorHelper::getContentFileInTemplate('router.php', 'list', false, true);
        AutogeneratorHelper::saveFile('router.php', $sContent, $sFrontendFolder);
        
        $sFrontendControllersFolder = $sFrontendFolder . DS . 'controllers';
        AutogeneratorHelper::createFolder($sFrontendControllersFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendControllersFolder);
        
        $sFrontendHelpersFolder = $sFrontendFolder . DS . 'helpers';
        AutogeneratorHelper::createFolder($sFrontendHelpersFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendHelpersFolder);
        
        $sFrontendModelsFolder = $sFrontendFolder . DS . 'models';
        AutogeneratorHelper::createFolder($sFrontendModelsFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendModelsFolder);
        
        $sFrontendTablesFolder = $sFrontendFolder . DS . 'tables';
        AutogeneratorHelper::createFolder($sFrontendTablesFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendTablesFolder);
        
        $sFrontendViewsFolder = $sFrontendFolder . DS . 'views';
        AutogeneratorHelper::createFolder($sFrontendViewsFolder);
        AutogeneratorHelper::copyIndexHTML($sFrontendViewsFolder);
        
        // Export tables.
        foreach ($aTables as $oTable) {
            $sContent = AutogeneratorHelper::getTableContent($oTable->id);
            AutogeneratorHelper::saveFile(strtolower($oTable->title) . '.php', $sContent, $sAdminTablesFolder);
            AutogeneratorHelper::saveFile(strtolower($oTable->title) . '.php', $sContent, $sFrontendTablesFolder);
        }
        
        $sContent = AutogeneratorHelper::getSqlInstallContent($iComponentId);
        AutogeneratorHelper::saveFile('install.mysql.utf8.sql', $sContent, $sAdminSqlFolder);
        
        $sContent = AutogeneratorHelper::getSqlUninstallContent($iComponentId);
        AutogeneratorHelper::saveFile('uninstall.mysql.utf8.sql', $sContent, $sAdminSqlFolder);
        
        foreach ($aMVCs as $oMVC) {
            switch ($oMVC->type) {
                case ConstantHelper::$_MVC_TYPE_FORM:
                    $sAdminViewFormFolder = $sAdminViewsFolder . DS . strtolower($oMVC->title);
                    AutogeneratorHelper::createFolder($sAdminViewFormFolder);
                    AutogeneratorHelper::copyIndexHTML($sAdminViewFormFolder);

                    $sContent = AutogeneratorHelper::getAdminFormViewHTMLContent($oMVC->id);
                    AutogeneratorHelper::saveFile('view.html.php', $sContent, $sAdminViewFormFolder);
                    
                    $sAdminViewFormTMPLFolder = $sAdminViewFormFolder . DS . 'tmpl';
                    AutogeneratorHelper::createFolder($sAdminViewFormTMPLFolder);
                    AutogeneratorHelper::copyIndexHTML($sAdminViewFormTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminFormEditContent($oMVC->id);
                    AutogeneratorHelper::saveFile('edit.php', $sContent, $sAdminViewFormTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminModelContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sAdminModelsFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminXMLFormContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.xml', $sContent, $sAdminModelsXMLFormsFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminControllerContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sAdminControllersFolder);
                    break;

                case ConstantHelper::$_MVC_TYPE_LIST:
                    // For administrator
                    $sAdminViewsListFolder = $sAdminViewsFolder . DS . strtolower($oMVC->title);
                    AutogeneratorHelper::createFolder($sAdminViewsListFolder);
                    AutogeneratorHelper::copyIndexHTML($sAdminViewsListFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminListViewHTMLContent($oMVC->id);
                    AutogeneratorHelper::saveFile('view.html.php', $sContent, $sAdminViewsListFolder);
                    
                    $sAdminViewsListTMPLFolder = $sAdminViewsListFolder . DS . 'tmpl';
                    AutogeneratorHelper::createFolder($sAdminViewsListTMPLFolder);
                    AutogeneratorHelper::copyIndexHTML($sAdminViewsListTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminListDefaultContent($oMVC->id);
                    AutogeneratorHelper::saveFile('default.php', $sContent, $sAdminViewsListTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminModelsContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sAdminModelsFolder);
                    
                    $sContent = AutogeneratorHelper::getAdminControllersContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sAdminControllersFolder);
                    
                    // For frontend
                    $sFrontendViewsListFolder = $sFrontendViewsFolder . DS . strtolower($oMVC->title);
                    AutogeneratorHelper::createFolder($sFrontendViewsListFolder);
                    AutogeneratorHelper::copyIndexHTML($sFrontendViewsListFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendListViewHTMLContent($oMVC->id);
                    AutogeneratorHelper::saveFile('view.html.php', $sContent, $sFrontendViewsListFolder);
                    
                    $sFrontendViewsListTMPLFolder = $sFrontendViewsListFolder . DS . 'tmpl';
                    AutogeneratorHelper::createFolder($sFrontendViewsListTMPLFolder);
                    AutogeneratorHelper::copyIndexHTML($sFrontendViewsListTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendListDefaultContent($oMVC->id);
                    AutogeneratorHelper::saveFile('default.php', $sContent, $sFrontendViewsListTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendModelsContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sFrontendModelsFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendControllersContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sFrontendControllersFolder);
                    break;
                
                case ConstantHelper::$_MVC_TYPE_ITEM:
                    $sFrontendViewItemFolder = $sFrontendViewsFolder . DS . strtolower($oMVC->title);
                    AutogeneratorHelper::createFolder($sFrontendViewItemFolder);
                    AutogeneratorHelper::copyIndexHTML($sFrontendViewItemFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendItemViewHTMLContent($oMVC->id);
                    AutogeneratorHelper::saveFile('view.html.php', $sContent, $sFrontendViewItemFolder);
                    
                    $sFrontendViewItemTMPLFolder = $sFrontendViewItemFolder . DS . 'tmpl';
                    AutogeneratorHelper::createFolder($sFrontendViewItemTMPLFolder);
                    AutogeneratorHelper::copyIndexHTML($sFrontendViewItemTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendItemDefaultContent($oMVC->id);
                    AutogeneratorHelper::saveFile('default.php', $sContent, $sFrontendViewItemTMPLFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendModelContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sFrontendModelsFolder);
                    
                    $sContent = AutogeneratorHelper::getFrontendControllerContent($oMVC->id);
                    AutogeneratorHelper::saveFile(strtolower($oMVC->title) . '.php', $sContent, $sFrontendControllersFolder);
                    
                    break;
                
            }
        }
        
        $sContent = '';
        foreach (AutogeneratorHelper::$aAdminLanguage as $sKey => $sValue) {
            $sContent .= $sKey . '="' . $sValue . '"' . chr(13);
        }
        AutogeneratorHelper::saveFile('en-GB.com_' . strtolower($oComponent->title) . '.ini', $sContent, $sComponentFolder . DS . 'administrator' . DS . 'language' . DS . 'en-GB');
        
        $sContent = '';
        foreach (AutogeneratorHelper::$aAdminLanguageSystem as $sKey => $sValue) {
            $sContent .= $sKey . '="' . $sValue . '"' . chr(13);
        }
        AutogeneratorHelper::saveFile('en-GB.com_' . strtolower($oComponent->title) . '.sys.ini', $sContent, $sComponentFolder . DS . 'administrator' . DS . 'language' . DS . 'en-GB');
        
        $sContent = '';
        foreach (AutogeneratorHelper::$aFrontendLanguage as $sKey => $sValue) {
            $sContent .= $sKey . '="' . $sValue . '"' . chr(13);
        }
        AutogeneratorHelper::saveFile('en-GB.com_' . strtolower($oComponent->title) . '.ini', $sContent, $sComponentFolder . DS . 'language' . DS . 'en-GB');
        
        $sContent = '';
        foreach (AutogeneratorHelper::$aFrontendLanguageSystem as $sKey => $sValue) {
            $sContent .= $sKey . '="' . $sValue . '"' . chr(13);
        }
        AutogeneratorHelper::saveFile('en-GB.com_' . strtolower($oComponent->title) . '.sys.ini', '', $sComponentFolder . DS . 'language' . DS . 'en-GB');
        
        AutogeneratorHelper::zipComponent($sComponentFolder, 'com_' . strtolower($oComponent->title) . '.zip');
        
        AutogeneratorHelper::deleteFolder($sComponentFolder);
        
        AutogeneratorHelper::forceDownload('com_' . strtolower($oComponent->title) . '.zip');
    }
    
}
